// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_CHK_QULITY_CHECKING_RESULT
// * CreateDate    : 2011-11-23 11:20:26
// * Creater       : CodeSmith
// * LastChangeDate: 2011-11-23 11:20:26
// * LastChanger   : 2011-11-23 11:20:26
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;
using ESOL.Common;

namespace ESOL.Business.Chk
{
    /// <summary>
    /// ChkQulityCheckingResult
    /// </summary>
    public partial class ChkQulityCheckingResultBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objChkQulityCheckingResultEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ChkQulityCheckingResultEntity GetChkQulityCheckingResult(ChkQulityCheckingResultEntity objChkQulityCheckingResultEntity)
        {
            return DataAccess.SelectSingle<ChkQulityCheckingResultEntity>(objChkQulityCheckingResultEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objChkQulityCheckingResultEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertChkQulityCheckingResult(ChkQulityCheckingResultEntity objChkQulityCheckingResultEntity)
        {
            return DataAccess.Insert(objChkQulityCheckingResultEntity);
        }

        public static bool InsertChkQulityCheckingResult(List<ChkQulityCheckingResultEntity> objChkQulityCheckingResultEntityList)
        {
            return DataAccess.Insert(objChkQulityCheckingResultEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objChkQulityCheckingResultEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateChkQulityCheckingResult(ChkQulityCheckingResultEntity objChkQulityCheckingResultEntity)
        {
            return DataAccess.Update(objChkQulityCheckingResultEntity);
        }

        public static bool UpdateChkQulityCheckingResult(List<ChkQulityCheckingResultEntity> objChkQulityCheckingResultEntityList)
        {
            return DataAccess.Update<ChkQulityCheckingResultEntity>(objChkQulityCheckingResultEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objChkQulityCheckingResultEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteChkQulityCheckingResult(ChkQulityCheckingResultEntity objChkQulityCheckingResultEntity)
        {
            return DataAccess.Delete(objChkQulityCheckingResultEntity);
        }

        public static bool DeleteChkQulityCheckingResult(List<ChkQulityCheckingResultEntity> objChkQulityCheckingResultEntityList)
        {
            return DataAccess.Delete<ChkQulityCheckingResultEntity>(objChkQulityCheckingResultEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetChkQulityCheckingResultAll()
        {
            return DataAccess.SelectAllDataSet<ChkQulityCheckingResultEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ChkQulityCheckingResultEntity> GetChkQulityCheckingResultAllList()
        {
            return DataAccess.SelectAll<ChkQulityCheckingResultEntity>();
        }
        #endregion

        #region Functions
        public static DataTable GetDtQcPurLastResultByQcOrderID(string QcOrderID)
        {
            if (string.IsNullOrEmpty(QcOrderID))
                return null;
            string Sql = @"SELECT CQCR.*,CQCID.* ,CQCID.QTY AS NG_QTY,MP.PRODUCT_CODE,MP.PRODUCT_NAME
                FROM CHK_QUALITY_CHECKING CQC
                LEFT JOIN CHK_QUALITY_CHECKING_ITEM CQCI ON CQC.CHECKING_ID=CQCI.CHECKING_ID
                LEFT JOIN CHK_QUALITY_CHECKING_ITEM_DETAIL CQCID ON CQCI.CHECKING_ITEM_ID=CQCID.CHECKING_ITEM_ID
                LEFT JOIN CHK_QULITY_CHECKING_RESULT CQCR ON CQCID.CHECKING_ITEM_DETAIL_ID=CQCR.CHECKING_ITEM_DETAIL_ID
                LEFT JOIN PUR_PURCHASE_ARRIVED_ITEM PPAI ON CQCI.RELATED_ITEM_ID=PPAI.ARRIVED_ITEM_ID
                LEFT JOIN PUR_PURCHASE_ORDER_ITEM PPOI ON PPAI.PURCHASE_ITEM_ID=PPOI.PURCHASE_ITEM_ID
                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID
                WHERE 1=1 AND CQCID.RESULT=1 AND CQC.CHECKING_ORDER_TYPE='QCOT01' AND CQC.CHECKING_ID='" + QcOrderID + "' ";
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static DataTable GetDtQcProdLastResultByQcOrderID(string QcOrderID)
        {
            if (string.IsNullOrEmpty(QcOrderID))
                return null;
            string Sql = @"SELECT CQCR.*,CQCID.* ,CQCID.QTY AS NG_QTY,MP.PRODUCT_CODE,MP.PRODUCT_NAME
                FROM CHK_QUALITY_CHECKING CQC
                LEFT JOIN CHK_QUALITY_CHECKING_ITEM CQCI ON CQC.CHECKING_ID=CQCI.CHECKING_ID
                LEFT JOIN CHK_QUALITY_CHECKING_ITEM_DETAIL CQCID ON CQCI.CHECKING_ITEM_ID=CQCID.CHECKING_ITEM_ID
                LEFT JOIN CHK_QULITY_CHECKING_RESULT CQCR ON CQCID.CHECKING_ITEM_DETAIL_ID=CQCR.CHECKING_ITEM_DETAIL_ID
                LEFT JOIN PROD_PLAN_ORDER_ITEM PPOI ON CQCI.RELATED_ITEM_ID=PPOI.PLAN_ORDER_ITEM_ID
                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID
                WHERE 1=1 AND CQCID.RESULT=1 AND CQC.CHECKING_ORDER_TYPE='QCOT02' AND CQC.CHECKING_ID='" + QcOrderID + "' ";
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static bool SaveApproveResult(ChkQualityCheckingEntity entity, DataTable dt)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    List<ChkQulityCheckingResultEntity> list = new List<ChkQulityCheckingResultEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        ChkQulityCheckingResultEntity m = new ChkQulityCheckingResultEntity(dr);
                        list.Add(m);
                    }
                    DataAccess.Insert(list, broker);
                    entity.Status = NormalStatus.STATUS03.ToString();
                    DataAccess.Update(entity,broker);

                    if (entity.CheckingOrderType.Equals("QCOT01"))
                    {
                        PurPurchaseArrivedEntity mPurchaseArrived = new PurPurchaseArrivedEntity();
                        mPurchaseArrived.ArrivedId = entity.RelatedOrderId;
                        mPurchaseArrived = DataAccess.SelectSingle<PurPurchaseArrivedEntity>(mPurchaseArrived);
                        mPurchaseArrived.IsQualitychecked = 1;
                        DataAccess.Update(mPurchaseArrived, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static DataTable GetLastResultQtyByCheckItemID(string CheckItemID,bool IsPass,string ProcessingMode)
        {
            if (string.IsNullOrEmpty(CheckItemID))
                return null;
            string Sql = @"SELECT SUM(CQCR.QTY) AS QTY 
                FROM dbo.CHK_QULITY_CHECKING_RESULT CQCR
                WHERE CQCR.CHECKING_ITEM_ID='"+CheckItemID+"'";
            if (IsPass)
                Sql += " AND CQCR.RESULT=0 ";
            else Sql += " AND CQCR.RESULT=1 ";
            if (!string.IsNullOrEmpty(ProcessingMode))
                Sql += " AND CQCR.PROCESSING_MODE='"+ProcessingMode+"' ";
            Sql+=" GROUP BY CQCR.QTY ";
            DataSet ds=DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }
        #endregion
    }


}

